package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-07-10
 * Time: 19:15
 */
public class Test {
    class ListNode{
        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;


    public void createList(){
        Test.ListNode node1 = new Test.ListNode(10);
        Test.ListNode node2 = new Test.ListNode(15);
        Test.ListNode node3 = new Test.ListNode(18);
        Test.ListNode node4 = new Test.ListNode(21);
        Test.ListNode node5 = new Test.ListNode(25);


        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }


    //遍历单链表
    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
    }

    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    public void addIndex(int index,int data){
        int size = size();
        if(index < 0||index > size){
            throw new IndexOutOfBounds("双向链表index下标不合法");
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size){
            addLast(data);
            return;
        }

        ListNode node = new ListNode(data);
        ListNode cur = head;
        while (index != 0){
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    public void remove(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        last = last.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }


    public void removeAllkey(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                if(cur == head){
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else{
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode node = new ListNode(-1);
        ListNode cur = node;
        while(list1 != null && list2 != null){
            if(list1.val >= list2.val){
                cur.next = list2;
                list2 = list2.next;
            }else{
                cur.next = list1;
                list1 = list1.next;
            }
            cur = cur.next;
        }
        while(list1 != null){
            cur.next = list1;
            list1 = list1.next;
            cur = cur.next;
        }
        while(list2 != null){
            cur.next = list2;
            list2 = list2.next;
            cur = cur.next;
        }

        return node.next;
    }

    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                if(bs == null){
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = cur;
                }
            }else{
                if(as == null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }

        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome(ListNode A) {

        // write code here
        if(A == null)return false;
        ListNode fast = A;
        ListNode slow = A;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        while(cur != null){
            ListNode mynode = cur.next;
            cur.next = slow;
            slow = cur;
            cur = mynode;

        }

        while(A != slow){
            if(A.val != slow.val){
                return false;
            }
            if(A.next == slow){
                return true;
            }
            A = A.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode head1 = headA;
        ListNode head2 = headB;
        int len1 = 0;
        int len2 = 0;
        while(head1 != null){
            head1 = head1.next;
            len1++;
        }
        while(head2 != null){
            head2 = head2.next;
            len2++;
        }
        head1 = headA;
        head2 = headB;
        int len = len1 - len2;
        if(len < 0){
            head1 = headB;
            head2 = headA;
            len = len2 - len1;
        }
        while(len != 0){
            head1 = head1.next;
            len--;
        }
        while(head1 != head2){
            head2 = head2.next;
            head1 = head1.next;
        }
        return head1;
    }
}
